# 🚀 一键运行所有测试 - 快速版
# 自动执行4个API测试，不需要手动确认

# 设置编码
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8

Write-Host "" 
Write-Host "=========================================" -ForegroundColor Cyan
Write-Host "  🚀 快速执行4个API测试" -ForegroundColor Cyan  
Write-Host "=========================================" -ForegroundColor Cyan
Write-Host "" 

$BASE_URL = "http://localhost:3001/api"
$testResults = @()

# 检查服务器
Write-Host "🔍 检查服务器状态..." -ForegroundColor Yellow
try {
    $null = Invoke-WebRequest -Uri "$BASE_URL/books" -Method Get -TimeoutSec 3 -ErrorAction Stop
    Write-Host "✅ 服务器运行正常！" -ForegroundColor Green
} catch {
    Write-Host "❌ 错误：无法连接到服务器！" -ForegroundColor Red
    Write-Host "请先启动服务器: npm run dev" -ForegroundColor Yellow
    exit 1
}

Write-Host "" 

# 测试 1: GET 所有图书
Write-Host "📷 测试 1: GET /api/books" -ForegroundColor Cyan
try {
    $response = Invoke-RestMethod -Uri "$BASE_URL/books?page=1&limit=10" -Method Get
    Write-Host "✅ 成功 - 找到 $($response.data.pagination.total) 本图书" -ForegroundColor Green
    $testResults += "✅ 测试1: 查询所有图书 - 通过"
} catch {
    Write-Host "❌ 失败: $($_.Exception.Message)" -ForegroundColor Red
    $testResults += "❌ 测试1: 查询所有图书 - 失败"
}

# 测试 2: POST 新增图书
Write-Host "📷 测试 2: POST /api/books" -ForegroundColor Cyan
$newBook = @{
    title = "Node.js 实战"
    author = "张三"
    publishDate = "2023-01-01"
    price = 99.99
    isbn = "9787111234567"
    publisher = "电子工业出版社"
    category = "编程技术"
    description = "深入浅出讲解 Node.js 开发技术"
    stock = 100
} | ConvertTo-Json

try {
    $response = Invoke-RestMethod -Uri "$BASE_URL/books" -Method Post -Body $newBook -ContentType "application/json"
    $bookId = $response.data._id
    Write-Host "✅ 成功 - 创建图书 ID: $bookId" -ForegroundColor Green
    $testResults += "✅ 测试2: 新增图书 - 通过"
    
    # 保存 Book ID
    $bookId | Out-File -FilePath "book-id-for-screenshots.txt" -Encoding UTF8
    
    # 测试 3: GET 单本图书
    Write-Host "📷 测试 3: GET /api/books/$bookId" -ForegroundColor Cyan
    try {
        $response = Invoke-RestMethod -Uri "$BASE_URL/books/$bookId" -Method Get
        Write-Host "✅ 成功 - 获取图书: $($response.data.title)" -ForegroundColor Green
        $testResults += "✅ 测试3: 查询单本图书 - 通过"
    } catch {
        Write-Host "❌ 失败: $($_.Exception.Message)" -ForegroundColor Red
        $testResults += "❌ 测试3: 查询单本图书 - 失败"
    }
    
    # 测试 4: DELETE 删除图书
    Write-Host "📷 测试 4: DELETE /api/books/$bookId" -ForegroundColor Cyan
    try {
        $response = Invoke-RestMethod -Uri "$BASE_URL/books/$bookId" -Method Delete
        Write-Host "✅ 成功 - 删除图书: $($response.data.title)" -ForegroundColor Green
        $testResults += "✅ 测试4: 删除图书 - 通过"
    } catch {
        Write-Host "❌ 失败: $($_.Exception.Message)" -ForegroundColor Red
        $testResults += "❌ 测试4: 删除图书 - 失败"
    }
    
} catch {
    Write-Host "❌ 失败: $($_.Exception.Message)" -ForegroundColor Red
    $testResults += "❌ 测试2: 新增图书 - 失败"
}

# 显示测试结果摘要
Write-Host "" 
Write-Host "=========================================" -ForegroundColor Cyan
Write-Host "  📊 测试结果摘要" -ForegroundColor Cyan
Write-Host "=========================================" -ForegroundColor Cyan
Write-Host "" 

foreach ($result in $testResults) {
    Write-Host $result
}

Write-Host "" 
Write-Host "✅ 所有测试执行完毕！" -ForegroundColor Green
Write-Host "" 
Write-Host "💡 提示：如需详细测试信息，请运行：" -ForegroundColor Yellow
Write-Host "   .\get-screenshot-data.ps1" -ForegroundColor White
Write-Host "" 
